<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="农户姓名" prop="realName">
        <el-input v-model="queryParams.realName" placeholder="请输入农户姓名" clearable />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入联系电话" clearable />
      </el-form-item>
      <el-form-item label="审核状态" prop="authStatus" style="width: 200px;">
        <el-select
          v-model="queryParams.authStatus"
          placeholder="请选择审核状态"
          clearable
        >
          <el-option label="待审核" value="0" />
          <el-option label="已通过" value="1" />
          <el-option label="已拒绝" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="authList">
      <el-table-column label="农户姓名" align="center" prop="realName" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="农场名称" align="center" prop="farmName" />
      <el-table-column label="农场执照编号" align="center" prop="farmLicense">
        <template #default="scope">
          {{ scope.row.farmLicense || "-" }}
        </template>
      </el-table-column>
      <el-table-column label="农场面积(亩)" align="center" prop="farmArea" />
      <el-table-column label="种植年限" align="center" prop="farmingYears" />
      <el-table-column label="主要作物" align="center">
        <template #default="scope">
          <el-tag
            v-for="crop in JSON.parse(scope.row.mainCrops)"
            :key="crop"
            size="small"
          >
            {{ getCropLabel(crop) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="authStatus">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.authStatus)">
            {{ getStatusText(scope.row.authStatus) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template #default="scope">
          <el-button type="primary" link icon="View" @click="handleDetail(scope.row)"
            >查看</el-button
          >
          <el-button
            v-if="scope.row.authStatus === '0'"
            type="success"
            link
            icon="Check"
            @click="handleAudit(scope.row)"
            >审核</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="handleQuery"
    />

    <!-- 查看详情对话框 -->
    <el-dialog :title="title" v-model="detailOpen" width="800px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="农户姓名">{{ form.farmName }}</el-descriptions-item>
        <el-descriptions-item label="联系电话">{{ form.phone }}</el-descriptions-item>
        <el-descriptions-item label="身份证号">{{
          form.idCardNumber
        }}</el-descriptions-item>
        <el-descriptions-item label="农场名称">{{ form.farmName }}</el-descriptions-item>
        <el-descriptions-item label="农场执照编号">{{
          form.farmLicense || "未填写"
        }}</el-descriptions-item>
        <el-descriptions-item label="农场面积"
          >{{ form.farmArea }}亩</el-descriptions-item
        >
        <el-descriptions-item label="种植年限"
          >{{ form.farmingYears }}年</el-descriptions-item
        >
        <el-descriptions-item label="所在地区">{{
          form.farmRegion
        }}</el-descriptions-item>
        <el-descriptions-item label="详细地址">{{
          form.farmAddress
        }}</el-descriptions-item>
      </el-descriptions>

      <!-- 图片资料展示 -->
      <div class="image-section">
        <h4>身份证照片</h4>
        <div class="image-group">
          <div class="image-item">
            <p>正面照片</p>
            <el-image :src="form.idCardImageFront" fit="contain" />
          </div>
          <div class="image-item">
            <p>反面照片</p>
            <el-image :src="form.idCardImageBack" fit="contain" />
          </div>
        </div>

        <h4>农场实景照片</h4>
        <div class="image-group">
          <div v-if="form.farmPhotos" class="farm-photos">
            <el-image
              v-for="(photo, index) in JSON.parse(form.farmPhotos || '[]')"
              :key="index"
              :src="photo"
              fit="cover"
              class="farm-photo"
            />
          </div>
        </div>

        <h4>其他证明材料</h4>
        <div v-if="form.otherDocs" class="docs-list">
          <el-link
            v-for="doc in JSON.parse(form.otherDocs || '[]')"
            :key="doc.id"
            type="primary"
            :href="doc.fileUrl"
            target="_blank"
          >
            {{ doc.fileName }}
          </el-link>
        </div>
      </div>
    </el-dialog>

    <!-- 审核对话框 -->
    <el-dialog :title="title" v-model="auditOpen" width="500px" append-to-body>
      <el-form
        ref="auditFormRef"
        :model="auditForm"
        :rules="auditRules"
        label-width="100px"
      >
        <el-form-item label="审核结果" prop="auditStatus">
          <el-radio-group v-model="auditForm.auditStatus">
            <el-radio label="1">通过</el-radio>
            <el-radio label="2">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="审核意见"
          prop="auditReason"
          v-if="auditForm.auditStatus === '2'"
        >
          <el-input
            v-model="auditForm.auditReason"
            type="textarea"
            placeholder="请输入拒绝原因"
            :rows="3"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitAudit">确 定</el-button>
          <el-button @click="auditOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { listAuth } from "@/api/farmer/auth";
import { updateStatus } from "@/api/farmer/authExt";
import { ElMessage } from "element-plus";
// 遮罩层
const loading = ref(false);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
// 弹出层标题
const title = ref("");
// 是否显示详情弹出层
const detailOpen = ref(false);
// 是否显示审核弹出层
const auditOpen = ref(false);

// 表单参数
const form = ref({});
// 认证列表
const authList = ref([]);

// 查询参数
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  realName: undefined,
  phone: undefined,
  authStatus: undefined,
});

// 审核表单参数
const auditForm = ref({
  id: undefined,
  auditStatus: "1",
  auditReason: "",
});

// 审核表单校验规则
const auditRules = {
  authStatus: [{ required: true, message: "请选择审核结果", trigger: "change" }],
  auditReason: [{ required: true, message: "请输入拒绝原因", trigger: "blur" }],
};

// 作物选项字典
const cropOptions = {
  rice: "水稻",
  wheat: "小麦",
  corn: "玉米",
  vegetable: "蔬菜",
  fruit: "水果",
};

onMounted(() => {
  getAuthList()
});

const getAuthList = async () => {
  const res = await listAuth(queryParams.value);
  authList.value = res.rows;
  total.value = res.total;
};
// 获取作物标签
const getCropLabel = (value) => {
  return cropOptions[value] || value;
};

// 获取状态类型
const getStatusType = (status) => {
  const statusMap = {
    0: "info",
    1: "success",
    2: "danger",
  };
  return statusMap[status];
};

// 获取状态文本
const getStatusText = (status) => {
  const statusMap = {
    0: "待审核",
    1: "已通过",
    2: "已拒绝",
  };
  return statusMap[status];
};

/** 搜索按钮操作 */
function handleQuery() {
  getAuthList()
  console.log("搜索参数:", queryParams.value);
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    realName: undefined,
    phone: undefined,
    authStatus: undefined,
  };
  getAuthList()
}

/** 查看详情按钮操作 */
function handleDetail(row) {
  title.value = "查看详情";
  detailOpen.value = true;
  form.value = row;
}

/** 审核按钮操作 */
function handleAudit(row) {
  title.value = "审核认证";
  auditOpen.value = true;
  auditForm.value = {
    id: row.id,
    auditStatus: "1",
    auditReason: "",
  };
}

/** 提交审核结果 */
async function submitAudit() {
  const res = await updateStatus(auditForm.value);
  if (res.code === 200) {
    ElMessage.success("操作成功");
    getAuthList()
  } else {
    ElMessage.error("操作失败");
  }
  auditOpen.value = false;
  //getAuthList()
}
</script>

<style lang="scss" scoped>
.image-section {
  margin-top: 20px;

  h4 {
    margin: 16px 0;
    padding-left: 10px;
    border-left: 4px solid #409eff;
    font-size: 16px;
  }

  .image-group {
    display: flex;
    gap: 20px;
    margin-bottom: 20px;

    .image-item {
      text-align: center;

      p {
        margin-bottom: 8px;
        color: #606266;
      }

      .el-image {
        width: 240px;
        height: 150px;
        border: 1px solid #ebeef5;
        border-radius: 4px;
      }
    }
  }

  .farm-photos {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;

    .farm-photo {
      width: 160px;
      height: 160px;
      border-radius: 4px;
      object-fit: cover;
    }
  }

  .docs-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 10px;
    background: #f8f9fa;
    border-radius: 4px;

    .el-link {
      display: inline-flex;
      align-items: center;

      &:hover {
        text-decoration: underline;
      }
    }
  }
}
</style>
